Ext.ns('WaterCheckForm0');
WaterCheckForm0 = Ext.extend(Ext.Window, {
	// 头部工具栏
	topbar : null,
	// 条件搜索Panel
	searchPanel : null,
	// 数据展示Panel
	gridPanel : null,
	// GridPanel的数据Store
	store : null,
	width : 1200,
	height : 520,
	// 构造函数
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		// 初始化组件
		this.initUIComponents();
		// 调用父类构造
		WaterCheckView.superclass.constructor.call(this, {
					id : 'WaterCheckForm0',
					title : '计划明细审核',
					width : 1000,
					region : 'center',
					layout : 'border',
					modal : true,
					plain : true,
					items : [this.searchPanel, this.gridPanel]
				});
	},
	// 初始化组件
	initUIComponents : function() {
		// 初始化搜索条件Panel
		this.searchPanel = new Ext.FormPanel({
					id : 'WaterSearchForm0',
					height : 100,
					region : 'north',
					frame : false,
					border : false,
					padding : '10',
					layout : 'column',
					title : '',
					labelWidth : 60,
					layoutConfig : {
						padding : '5',
						align : 'middle'
					},
					defaults : {
						columnWidth : 0.33,
						layout : 'form',
						border : false,
						style : 'padding-left:10px;',
						defaults : {
							xtype : 'textfield',
							width : 130
						}
					},
					items : [{
								items : [{
											fieldLabel : '计划名称',
											name : 'Q_planName_S_EQ',
											width : 200,
											value : this.planName,
											readOnly: true,
											style:'color:#666666'
										}, {
											fieldLabel : '水表编号',
											id : 'pdaWaterMeterCheck.wmId',
											name : 'Q_amId_S_LK'
										},{
											fieldLabel : '房',
											width : 220,
											hiddenName : 'aparmentId_S_LK',
											xtype : 'combo',
											id : 'pdaWaterMeterCheck.houseID',
											valueField : 'houseId',
											displayField : 'houseName',
											mode : 'local',
											editable : false,
											triggerAction : 'all',
											store : new Ext.data.SimpleStore({
														url : __ctxPath+ '/pdaweb/showHouseWaterMeterPlan.do?buildingId=',
														//autoLoad : true,
														fields : ['houseId','houseName']
													}),
											listeners : {
												scope : this,
												select : function(cbo,record, index) {
												}
											}
										
										
										}]
							}, {
								items : [{
									        fieldLabel : '抄表年月',
									        name : 'Q_month_S_EQ',
									        value : this.lastEditBy,
											readOnly: true,
											style:'color:#666666'
											
										},{
											fieldLabel : '区',
											hiddenName : 'districtId_S_LK',
											xtype : 'combo',
											id : 'pdaWaterMeterCheck.districtID',
											valueField : 'districtId',
											displayField : 'districtName',
											mode : 'local',
											editable : false,
											triggerAction : 'all',
											store : new Ext.data.SimpleStore({
														url : __ctxPath
																+ '/pdaweb/showDistrictAmPlan.do',
														autoLoad : true,
														fields : ['districtId','districtName']
													}),
											listeners : {
												scope : this,
												select : function(cbo,record, index) {
													var b = Ext.getCmp('pdaWaterMeterCheck.buildingID');
													var h = Ext.getCmp('pdaWaterMeterCheck.houseID');
													b.setValue('');
													b.store.loadData([]);
													h.setValue('');
													h.store.loadData([]);
													chooseDistrictId = cbo.getValue();
													Ext.Ajax.request({
														url : __ctxPath+ '/pdaweb/showBuildingAmPlan.do?districtId='
																+ chooseDistrictId,
														method : 'post',
														success : function(response,option) {
															b.store.loadData(eval("("+ response.responseText
																			+ ")"));
														}
													});
												}
											}
											
										},{
											xtype : 'button',
											text : '查询',
											region : 'center',
											width : 60,
											iconCls : 'search',
											style : 'margin-center:10px',
											scope : this,
											handler : function(){
												var searchPanel = Ext.getCmp('WaterSearchForm0');
												var gridPanel = Ext.getCmp('WaterGridPanelForm0');
												if (searchPanel.getForm().isValid()) {
													$search({
														searchPanel : searchPanel,
														gridPanel : gridPanel
													});
												}
											}
										}]
							},{
								items : [{
											fieldLabel : '抄表人',
											name : 'Q_executor_S_EQ',
											value : this.executor,
											readOnly: true,
											style:'color:#666666'
									
										},{
											fieldLabel : '栋',
											hiddenName : 'buildingId_S_LK',
											xtype : 'combo',
											id : 'pdaWaterMeterCheck.buildingID',
											valueField : 'buildingId',
											displayField : 'buildingtName',
											mode : 'local',
											editable : false,
											triggerAction : 'all',
											store : new Ext.data.SimpleStore({
														url : __ctxPath+ '/pdaweb/showBuildingAmPlan.do?districtId=',
														//autoLoad : true,
														fields : ['buildingId','buildingtName']
													}),
											listeners : {
												scope : this,
												select : function(cbo,record, index) {
													var h = Ext.getCmp('pdaWaterMeterCheck.houseID');
													h.setValue('');
													h.store.loadData([]);
													chooseBulidingId = cbo.getValue();
													Ext.Ajax.request({
														url : __ctxPath+ '/pdaweb/showHouseWaterMeterPlan.do?buildingId='
																+ chooseBulidingId,
														method : 'post',
														success : function(response,option) {
															h.store.loadData(eval("("+ response.responseText+ ")"));
															}
													});
												}
											}
										},{
											style : 'margin-top:4px;',
											width : 60,
					    					xtype : 'button',
					    					text : '重置',
					    					iconCls : 'btn-reset',
					    					handler : function() {
					    						Ext.getCmp('pdaWaterMeterCheck.wmId').setValue('');
					    						Ext.getCmp('pdaWaterMeterCheck.districtID').setValue('');
					    						Ext.getCmp('pdaWaterMeterCheck.buildingID').setValue('');
					    						Ext.getCmp('pdaWaterMeterCheck.houseID').setValue('');
					    						//Ext.getCmp('WaterSearchForm0').getForm().reset();
					    					}
										}]
								
							}, {
								items : [{
											xtype : 'hidden',
											name : 'Q_status_S_EQ',
											value : '1'
										}]
							},{
								items : [{
									xtype:'hidden',
									name : 'Q_planId_S_EQ',
									value: this.planId
								}]}]
				});// end of the searchPanel
		
		this.store = new Ext.data.JsonStore({
					url : __ctxPath+  '/pdaweb/checkDetailSearchPdaWaterList.do',
					root : 'result',
					totalProperty : 'totalCounts',
					fields : [{
						name : 'id',
						type : 'int'
					}, 'status', 'amId', 'lastDegree', 'degree', 'fixedUsage', 'usage',
					'pdaWaterMeter.districtId',
					'pdaWaterMeter.buildingId',
					'pdaWaterMeter.aparmentId', 'recorderId', 'month','remark','createddt'],
					remoteSort : true
				});
		
		this.store.baseParams = {
				Q_planId_S_EQ : this.planId,
				Q_month_S_EQ :this.lastEditBy,
				Q_amId_S_LK : "",
				districtId_S_LK : "",
				buildingId_S_LK : "",
				aparmentId_S_LK : "",
				Q_amId_S_LK :""	,
				Q_planName_S_EQ : this.planName
		}
		
		this.store.load({
				params : {
					start : 0,
					limit : 12									
				}
			});

		var sm = new Ext.grid.CheckboxSelectionModel({
            handleMouseDown:Ext.emptyFn,
            listeners:{'beforerowselect': function(s,i,k,r ) {
                      return r.get("status")!='2';
                }
            },
            renderer : function(v,c,r){
	            if(r.get("status")=='2'){
	                return "<div style='padding:3px 0 0 3px;'><input type='checkbox' disabled /></div>";
	            }else{
	                return "<div class=\"x-grid3-row-checker\">&#160;</div>";
	            }
            }            
        });
		var cm = new Ext.grid.ColumnModel({
			columns : [sm, new Ext.grid.RowNumberer(), 
			           {
						header : '抄表人',
						width : 60,
						dataIndex : 'recorderId'
					},{
						header : '水表编号',
						width : 120,
						dataIndex : 'amId'
					},{
						header : '区号',
						width : 60,
						dataIndex : 'pdaWaterMeter.districtId'
					},{
						header : '栋号',
						width : 100,
						dataIndex : 'pdaWaterMeter.buildingId'
					},{
						header : '房号',
						width : 150,
						dataIndex : 'pdaWaterMeter.aparmentId'
					},{
						header : '本月读数',
						width : 60,
						dataIndex : 'degree'
					},{
						header : '上月读数',
						width : 60,
						dataIndex : 'lastDegree', // lastdegree
						renderer : function(val) {
							return val == -1 ? "" : val;
						}
					},{
						header : '本月用量',
						width : 60,
						dataIndex : 'usage'
					},{
						header : '修正量',
						width : 50,
						dataIndex : 'fixedUsage'
					},{
						header : '备注',
						width : 150,
						dataIndex : 'remark'
					},{
						header : '操作',
						width : 50,
						dataIndex : 'id',
						sortable : false,
						renderer : function(value, metadata, record, rowIndex,
								colIndex) {
							var d = record.data;
							var str = '';
							str += '&nbsp;<button title="修正上月读数" value="" class="btn-edit" '
								+ 'onclick="WaterCheckForm0.fix('
								+ record.data.id
								+ ');">&nbsp</button>';
							return str;
						}
					}],
			defaults : {
				sortable : true,
				menuDisabled : false
			}
		});// end of the cm

		this.topbar = new Ext.Toolbar({
					id : 'WaterFootBar',
					height : 25,
					bodyStyle : 'text-align:left',
					items : [{
						xtype : 'button',
						text : '审核',
						region : 'center',
						width : 60,
						iconCls : 'btn-add',
						style : 'margin-center:10px',
						scope : this,
						handler : function() {
							var grid = Ext.getCmp("WaterGridPanelForm0");
							var selectRecords = grid.getSelectionModel().getSelections();
							if (selectRecords.length == 0) {
								Ext.ux.Toast.msg("信息", "请选择记录！");
								return;
							}
							var warnIds = '';
							var waitIds = '';
							for (var i = 0; i < selectRecords.length; i++) {
								var st = selectRecords[i].data.status;
								if ('WAIT' == WaterCheckForm0.getST(st)) {
									waitIds += selectRecords[i].data.id + ',';
								} else if ('WARN' == WaterCheckForm0.getST(st)) {
									warnIds += selectRecords[i].data.id + ',';
								}
							}

							if (warnIds != '') {
								Ext.Msg.confirm('提示',
										'选择中包含黄颜色警示记录,你确定要审核通过吗？', function(
												btn, text) {
											if (btn == 'yes') {
												WaterCheckForm0.check(waitIds
														+ warnIds);
											} else {
												return false;
											}
										});
							} else {
								if (waitIds != '') {
									WaterCheckForm0.check(waitIds)
								}
							}
						}

					}]
					
				});

		this.gridPanel = new Ext.grid.GridPanel({
					id : 'WaterGridPanelForm0',
					region : 'center',
					store : this.store,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					autoHeight : true,
					tbar : this.topbar,
					cm : cm,
					sm : sm,
					viewConfig : {
						showPreview : false,
						getRowClass : function(record, rowIndex, rowParams,store) {// 异常状态
							var st = record.data.status;
							if ('ERROR' == WaterCheckForm0.getST(st)) {
								return 'x-grid-record-error';
							} else if ('WARN' == WaterCheckForm0.getST(st)) {
								return 'x-grid-record-warning';
							} else {
								return '';
							}
						}
					},
					bbar : new Ext.PagingToolbar({
								pageSize : 12,
								store : this.store,
								displayInfo : true,
								displayMsg : '当前显示从{0}至{1}， 共{2}条记录',
								emptyMsg : "当前没有记录"
							})
				});
	}
});

//修正
WaterCheckForm0.fix = function(id) {
 new FixForm({id : id}).show();
}

WaterCheckForm0.getST = function(st) {
	return st == '0' ? 'WAIT' : st == '2' ? 'ERROR' : st == '3' || st == '4'
			? 'WARN'
			: 'OTHER';
}

//审核
WaterCheckForm0.check = function(ids) {
	Ext.Ajax.request({
		url : __ctxPath
				+ '/pdaweb/checkPdaWaterList.do?fileName='
				+ ids,
		waitMsg : '正在提交数据...',
		timeout: 30000,		
		method : 'post',
		success : function(response) {
			Ext.ux.Toast.msg("操作信息", "审核成功");
			Ext.getCmp('WaterGridPanelForm0').getStore().reload();
		},
		failure : function() {
//			Ext.Msg.alert("操作信息", "审核失败");
			Ext.MessageBox.show({
				title : '操作信息',
				msg : '审核失败',
				buttons : Ext.MessageBox.OK,
				icon : 'ext-mb-error'
			});
		}
	});
}
